﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommunitySystem
{
    public partial class UserManagerControl : UserControl
    {
        SystemService.SystemServiceClient Client = new SystemService.SystemServiceClient();
        SystemService.UserInfo UserInfoModel = new SystemService.UserInfo();
        public UserManagerControl()
        {
            InitializeComponent();

            ///
            this.Loaded += new RoutedEventHandler(UserManagerControl_Loaded);

        }

        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void UserManagerControl_Loaded(object sender, RoutedEventArgs e)
        {
            //throw new NotImplementedException();
            LoadData();
        }
        /// <summary>
        /// 加载数据
        /// </summary>
        void LoadData()
        {
            Client.GetUserInfoCompleted += new EventHandler<SystemService.GetUserInfoCompletedEventArgs>(Client_GetUserInfoCompleted);
            Client.GetUserInfoAsync();
        }
        /// <summary>
        /// 绑定数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Client_GetUserInfoCompleted(object sender, SystemService.GetUserInfoCompletedEventArgs e)
        {
            DGUser.ItemsSource = null;
            DGUser.ItemsSource = e.Result;

        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NewData_Click(object sender, RoutedEventArgs e)
        {
            UserManagerWindow UMWindow = new UserManagerWindow();
            UMWindow.Closed += new EventHandler(UMWindow_Closed);
            UMWindow.Show();
            ///注册关闭事件
           
        }
        /// <summary>
        /// 关闭后调用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void UMWindow_Closed(object sender, EventArgs e)
        {
            LoadData();
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EditData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGUser.SelectedItem as SystemService.UserInfo;
            
            //throw new NotImplementedException();
            if (seljw != null)
            {
                UserManagerWindow UMWindow = new UserManagerWindow(seljw);
                ///注册关闭事件
                UMWindow.Closed += new EventHandler(UMWindow_Closed);
                UMWindow.Show();
                
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteData_Click(object sender, RoutedEventArgs e)
        {
            //判断选中项目
            var seljw = this.DGUser.SelectedItem as SystemService.UserInfo;

            if (seljw != null)
            {
                Client.DeleteUserCompleted -= new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(Client_DeleteUserCompleted);
                Client.DeleteUserCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(Client_DeleteUserCompleted);
                Client.DeleteUserAsync(seljw.UserID);
            }
        }

        /// <summary>
        /// 删除后加载数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Client_DeleteUserCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            //throw new NotImplementedException();
            SilverlightMessageBoxLibrary.Message.InfoMessage("删除成功！");
            LoadData();
        }
    }
}
